/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.actions;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.ipo.mm.ejb.facade.user.UserFacadeLocal;
import org.ipo.mm.ejb.entities.User;
import org.ipo.mm.web.formbeans.LoginBean;
import org.ipo.mm.web.util.BreadCrumbs;
import org.ipo.mm.web.util.ConstantManager;

/**
 *
 * @author makcro
 */
public class LoginAction extends org.apache.struts.action.Action {
   
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ActionForward forward = null;

        LoginBean loginForm = (LoginBean) form;

        String username = loginForm.getUsername();
        String password = loginForm.getPassword();

        UserFacadeLocal userFacade = lookupUserFacade();

        Boolean result = userFacade.validate(username, password);

        if(result){

            User user = userFacade.find(username);

            request.getSession().setAttribute(ConstantManager.SESSION_USER_PARAM_NAME, user);
            request.getSession().setAttribute(ConstantManager.SESSION_BREADCRUMB_PARAM_NAME, new BreadCrumbs());

            //set current user privileges
            org.ipo.mm.web.util.security.SecurityManager securityManager = new org.ipo.mm.web.util.security.SecurityManager();
            Collection<Integer> privileges = securityManager.getPrivilegesByUserType(user.getType());

            request.getSession().setAttribute(ConstantManager.SESSION_USER_PRIVILEGES, privileges);

            // set form resetgin setings
            request.getSession().setAttribute(ConstantManager.SESSION_INST_RESET_FORM, new Boolean(true));
            request.getSession().setAttribute(ConstantManager.SESSION_REPR_RESET_FORM, new Boolean(true));
            request.getSession().setAttribute(ConstantManager.SESSION_MEETING_RESET_FORM, new Boolean(true));


           forward = mapping.findForward(ConstantManager.NAVIGATION_LOGIN_SUCCESSFULL);

        } else {

            ActionMessages errors = new ActionMessages();
            ActionMessage error = new ActionMessage("errors.invalidLogin");

            errors.add("username", error);

            saveErrors(request, errors);
            forward = mapping.getInputForward();

        }

        return forward;
    }

    private UserFacadeLocal lookupUserFacade() {
        try {
            Context c = new InitialContext();
            return (UserFacadeLocal) c.lookup("java:comp/env/UserFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
